package schedule.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.event.ActionEvent;

public class DriverBean {

	private UIForm form;
	private UIForm tableForm;
	private UICommand addCommand;

	public InputHr[] hour;
	public InputMin[] minutes;
	public String[] daysOfWeek;
	public String currentStartHour = "800";
	public String currentStartMin = "0";
	public String currentEndHour = "900";
	public String currentEndMin = "0";
	public String currentDay="0";
	private InputTime addedTime;
	private List<InputHr> hrList;
	private List<InputTime> allTimes;
	private List<InputTime> studentTimes;
	private DataRequester requester;
	private String selectedName;
	public List<String> concatAdvisors;
	public List<Advisor> advisorList;
	public Advisor currentAdvisor;	
	private HashMap<String, Student> freeStudentList;

	public DriverBean(){
		hour = new InputHr[11];
		hour[0] = new InputHr("8am", 8);
		hour[1] = new InputHr("9am", 9);
		hour[2] = new InputHr("10am", 10);
		hour[3] = new InputHr("11am", 11);
		hour[4] = new InputHr("12pm", 12);
		hour[5] = new InputHr("1pm", 1);
		hour[6] = new InputHr("2pm", 2);
		hour[7] = new InputHr("3pm", 3);
		hour[8] = new InputHr("4pm", 4);
		hour[9] = new InputHr("5pm", 5);
		hour[10] = new InputHr("6pm", 6);
		minutes = new InputMin[4];
		minutes[0] = new InputMin("0", 00);
		minutes[1] = new InputMin("15", 15);
		minutes[2] = new InputMin("30", 30);
		minutes[3] = new InputMin("45", 45);
		daysOfWeek = new String[5];
		daysOfWeek[0]="0";
		daysOfWeek[1]="1";
		daysOfWeek[2]="2";
		daysOfWeek[3]="3";
		daysOfWeek[4]="4";

		allTimes = new ArrayList<InputTime>();	
		studentTimes = new ArrayList<InputTime>();
		

		concatAdvisors = new ArrayList<String>();
		requester = new DataRequester();
		advisorList = requester.getAdvisorNames();
		
		for (Advisor adv : advisorList){
			concatAdvisors.add(String.format(adv.firstName + " " + adv.lastName));
		}
		//mailSender = new MailHandler();

		currentAdvisor = new Advisor(2, "Dan", "Palmer");
		freeStudentList = new HashMap<String, Student>();
	}

	public String addNew() {
		form.setRendered(true);
		addCommand.setRendered(true);
		return null;
	}
	//add new inputTime to the local ArrayList<inputTime>
	public String save() {
		addedTime = new InputTime(Integer.parseInt(currentStartHour),
				Integer.parseInt(currentStartMin),
				Integer.parseInt(currentEndHour),
				Integer.parseInt(currentEndMin), calculateDayNum(currentDay));		
		allTimes.add(addedTime);
		System.out.println("added startHour " + currentStartHour
				+ " and endHour " + currentEndHour + " on day " + currentDay);
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}
	public String pickAdvisor() {
		
		for(Advisor a : advisorList){
			String advisorName = a.firstName + " " + a.lastName;
			if (advisorName.equals(selectedName))
				currentAdvisor = a;
		}
		System.out.println(currentAdvisor.id);
		return "chosen";
	}
	public String cancel() {
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}

	public String delete() {
		allTimes.remove(addedTime);
		return null;
	}
	//submits arrayList to dataRequester to insert in database
	public String submit() {
		System.out.println("Submitting arrayList of size " + allTimes.size());
		requester.insertIntoAdvisor(allTimes, currentAdvisor.id);
		return null;
	}
	
	public String scheduleMeeting(){
		//Change to find what the advisor id is
		pullCompatibleTimes(currentAdvisor.id);
		return null;
	}

	public void pullCompatibleTimes(int advisorID){			
		ArrayList<InputTime> studentTimes = requester.getStudentTimes(advisorID);
		ArrayList<InputTime> advisorTimes = requester.getAdvisorTimes(advisorID);
		freeStudentList = generateListOfTimes(studentTimes, advisorTimes);
				
	}
	private HashMap<String, Student> generateListOfTimes(ArrayList<InputTime> studentTimes, ArrayList<InputTime> advisorTimes){		
		HashMap<String, Student> studentList = new HashMap<String, Student>();		
		//goes through arrayList of inputTimes, creates a Student() for every unique bannerID, adds them to the list. 
		//if student already exists, update their available times
		for (InputTime a: studentTimes){
			String thisBannerID = a.bannerID;
			if (!studentList.containsKey(a.bannerID)){				
				Student newStudent = new Student(thisBannerID);
				newStudent.addTime(a);
				studentList.put(thisBannerID, newStudent);
			}else{
				Student existingStudent = studentList.get(thisBannerID);
				existingStudent.addTime(a);
				studentList.put(thisBannerID, existingStudent);
			}
		}
		//adds available times to the advisor object				
		for (InputTime b: advisorTimes){			
			if(b !=null) currentAdvisor.addTime(b);
		}
		//generate milTime and boolSchedules
		FinalSchedule fs = new FinalSchedule();
		currentAdvisor.createSchedules();		 
		// need to get minMeetTime from web page and insert here
		for (Student s : studentList.values()){
			s.createSchedules();
			//generates a compatible schedule based on advisor's schedule and student's, and stores them in student obj			
			s.monCom = fs.generateResultMilSchedule(s.monMil, currentAdvisor.monMil);
			s.tueCom = fs.generateResultMilSchedule(s.tueMil, currentAdvisor.tueMil);
			s.wedCom = fs.generateResultMilSchedule(s.wedMil, currentAdvisor.wedMil);
			s.thuCom = fs.generateResultMilSchedule(s.thuMil, currentAdvisor.thuMil);
			s.friCom = fs.generateResultMilSchedule(s.friMil, currentAdvisor.friMil);
			s.compatibleTimes = s.compatInputTimes();
			studentList.put(s.bannerID, s);
		}			
		studentList = requester.getStudentEmails(studentList);				
		//mailSender.sendEmail(studentList);
		return studentList;
	}
	//set milTime

	/*
	 * private sendOutEmails(HashMap<String, Student> studentList)
	 * 
	 */
	public void displayTable(ActionEvent event) {
		if (event.getComponent().getId().equalsIgnoreCase("hide")) {
			tableForm.setRendered(false);
		} else {
			tableForm.setRendered(true);
		}
	}

	public int calculateDayNum(String day) {
		int dayNum = 0;
		if (day.equals("M"))
			dayNum = 0;
		else if (day.equals("T"))
			dayNum = 1;
		else if (day.equals("W"))
			dayNum = 2;
		else if (day.equals("R"))
			dayNum = 3;
		else if (day.equals("F"))
			dayNum = 4;
		return dayNum;
	}

	// Crazy number of getters and setters
	public InputHr[] getHourList() {
		return hour;
	}

	public InputMin[] getMinList() {
		return minutes;
	}

	public String getCurrentEndHour() {
		return currentEndHour;
	}

	public void setCurrentEndHour(String currentEndHour) {
		this.currentEndHour = currentEndHour;
	}

	public String getCurrentEndMin() {
		return currentEndMin;
	}

	public void setCurrentEndMin(String currentEndMin) {
		this.currentEndMin = currentEndMin;
	}

	public String[] getDaysOfWeek() {

		daysOfWeek = new String[5];
		daysOfWeek[0] = "M";
		daysOfWeek[1] = "T";
		daysOfWeek[2] = "W";
		daysOfWeek[3] = "R";
		daysOfWeek[4] = "F";
		return daysOfWeek;
	}
	public void setCurrentDay(String currentDay) {
		this.currentDay = currentDay;
	}

	public String getCurrentDay() {
		return currentDay;
	}

	public List<InputHr> getHrList() {
		return hrList;
	}

	public UIForm getForm() {
		return form;
	}

	public void setForm(UIForm form) {
		this.form = form;
	}

	public UICommand getAddCommand() {
		return addCommand;
	}

	public void setAddCommand(UICommand addCommand) {
		this.addCommand = addCommand;
	}

	public UIForm getTableForm() {
		return tableForm;
	}

	public void setTableForm(UIForm tableForm) {
		this.tableForm = tableForm;
	}

	public String getCurrentStartHour() {
		return currentStartHour;
	}

	public void setCurrentStartHour(String currentHour) {
		this.currentStartHour = currentHour;
	}

	public String getCurrentStartMin() {
		return currentStartMin;
	}

	public void setCurrentStartMin(String currentMin) {
		this.currentStartMin = currentMin;
	}

	public List<InputTime> getAllTimes() {
		return allTimes;
	}
	public List<InputTime> getStudentTimes() {
		
		studentTimes.add(0, new InputTime(12,00,1,00,0));
		studentTimes.add(1, new InputTime(2,00,3,00,0));
		studentTimes.add(2, new InputTime(4,10,5,00,0));
		//getTimesForBanner(String bannerID);
		return studentTimes;
	}
	public ArrayList<InputTime> getTimesForBanner(String bannerID){
		ArrayList<InputTime> returnList = new ArrayList<InputTime>();
		Student correctStudent = freeStudentList.get(bannerID);
		returnList = correctStudent.compatibleTimes;
		return returnList;
	}

	public void setAllTimes(List<InputTime> allTimes) {
		this.allTimes = allTimes;
	}

	public InputTime getAddedTime() {
		return addedTime;
	}

	public void setAddedTime(InputTime addedTime) {
		this.addedTime = addedTime;
	}

	public List<String> getConcatAdvisors() {
		return concatAdvisors;
	}

	public void setConcatAdvisors(List<String> concatAdvisors) {
		this.concatAdvisors = concatAdvisors;
	}

	public String getSelectedName() {
		return selectedName;
	}

	public void setSelectedName(String selectedName) {
		this.selectedName = selectedName;
	}
}
